package com.example.service;

import com.example.filter.JWTAuthenticationFilter;
import com.example.utils.JWTUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

@Service
public class SysTokenServiceImpl implements SysTokenService {

    /****
     * 校验请求的TOKEN是否存在，是否准确，是否已经过期。
     * @param request
     * @param response
     * @return
     */
    @Override
    public boolean verify(ServletRequest request, ServletResponse response) {
        boolean flag = true;
        String token = ((HttpServletRequest) request).getHeader(JWTAuthenticationFilter.TOKEN);
        if (StringUtils.isEmpty(token)) {
            flag = false;
        } else {
            return JWTUtils.verifyToken(token);
        }
        return flag;
    }
}
